package org.op.editor;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class TextFileFilter extends FileFilter {

	@Override
	public boolean accept(File f) {
		
		boolean accept = false;
		
	    if (f.isDirectory()) {
	    	accept = true;
	    }

        String extension = getExtension(f);
        if (extension != null) {
	    	if (extension.equals("txt")) {
	    	   accept = true;
	    	}
    	}
        
        return accept;
	}

	@Override
	public String getDescription() {
		return "Text (.txt) files only";
	}
	
    /*
     * Get the extension of a file.
     */  
    private String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }

}
